package com.jader.activities;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.jader.main.MainActivity;
import com.jader.myviews.SearchClearEditText;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SouSuoActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	private ImageButton ib_back_normal;
	private TextView tv_search;
	private SearchClearEditText clear_et;
	private ListView lv_search_history;
	private Button clear_search_history_btn;
	// 存放搜索到的到的商品
	List<String> list = new ArrayList<String>();
	private String goodsname;
	private SQLiteDatabase sqLiteDatabase;
	private String text;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sou_suo);
		inintView();

	}

	private void inintView() {
		ib_back_normal = (ImageButton) findViewById(R.id.ib_back_normal);
		tv_search = (TextView) findViewById(R.id.tv_search);
		// 搜索框
		clear_et = (SearchClearEditText) findViewById(R.id.et_clear);
		clear_search_history_btn = (Button) findViewById(R.id.clear_search_history_btn);

		lv_search_history = (ListView) findViewById(R.id.search_history_lv);

		ib_back_normal.setOnClickListener(this);
		tv_search.setOnClickListener(this);
		clear_search_history_btn.setOnClickListener(this);
		lv_search_history.setOnItemClickListener(this);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, list);

		// 加上listview头部
		/* lv_search_history.addHeaderView(hearderViewLayout); */
		lv_search_history.setAdapter(adapter);
		select();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// 监听单击事件
		String result = list.get(arg2);
		try {
			goodsname = URLEncoder.encode(result, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Intent intent1 = new Intent(SouSuoActivity.this,
				SouDetailActivity.class);
		intent1.putExtra("goodsname", goodsname);
		startActivity(intent1);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_back_normal:
			Intent intent = new Intent(SouSuoActivity.this, MainActivity.class);
			startActivity(intent);
			SouSuoActivity.this.finish();

			break;
		case R.id.tv_search:
			text = clear_et.getText().toString().trim();
			if (text == null || "".equals(text)) {
				Toast.makeText(this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
			} else {
				try {
					goodsname = URLEncoder.encode(text, "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				insert(text);
				Intent intent1 = new Intent(SouSuoActivity.this,
						SouDetailActivity.class);
				intent1.putExtra("goodsname", goodsname);
				startActivity(intent1);
			}
			break;

		case R.id.clear_search_history_btn:
			deleteHistory();
			break;
		default:
			break;
		}

	}

	private void deleteHistory() {
		sqLiteDatabase = JaderApplication.getSqLiteDatabase();
		String sql = "delete from search";
		sqLiteDatabase.execSQL(sql);
		sqLiteDatabase.close();
		// 删除搜索的商品
		deleteDatabase(goodsname);
		list.clear();
		adapter.notifyDataSetChanged();
	}

	public void insert(String text) {

		sqLiteDatabase = JaderApplication.getSqLiteDatabase();
		String sql = "insert into search (name) values (?)";
		String[] texts = { text };
		// 判断如果添加的内容不为空 则执行添加语句
		if (texts != null) {
			sqLiteDatabase.execSQL(sql, texts);
		}
		sqLiteDatabase.close();
	}

	public void select() {
		sqLiteDatabase = JaderApplication.getSqLiteDatabase();
		String sql = "select * from search";
		Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			String msg = cursor.getString(0);
			list.add(msg);
		}
		sqLiteDatabase.close();
		adapter.notifyDataSetChanged();
	}

}
